package com.demo;
import org.apache.flink.table.functions.ScalarFunction;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 转换时间格式
 */
public class TimeFormatFunction extends ScalarFunction {
    public String DATE_FORMAT;

    public TimeFormatFunction() {
        this.DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";
    }

    public TimeFormatFunction(String dateFormat) {
        this.DATE_FORMAT = dateFormat;
    }

    public String eval(String dateTime) {
        try {
            dateTime = dateTime.replace("Z", " UTC");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            Date d = format.parse(dateTime);
            return format2.format(d);
        } catch (Exception e) {
            return null;
        }
    }
}
